package ee.ut.math.tvt.salessystem.ui.model;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import ee.ut.math.tvt.salessystem.domain.data.Sale;
import ee.ut.math.tvt.salessystem.domain.data.SoldItem;
import ee.ut.math.tvt.salessystem.domain.data.StockItem;

/**
 * Purchase history details model.
 */
public class PurchaseInfoTableModel extends SalesSystemTableModel<SoldItem> {
	private static final long serialVersionUID = 1L;

	private static final Logger log = Logger.getLogger(PurchaseInfoTableModel.class);

	public PurchaseInfoTableModel() {
		super(new String[] { "Id", "Name", "Price", "Quantity", "Sum" });
	}

	@Override
	protected Object getColumnValue(SoldItem item, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return item.getId();
		case 1:
			return item.getName();
		case 2:
			return item.getPrice();
		case 3:
			return item.getQuantity();
		case 4:
			return item.getSum();
		}
		throw new IllegalArgumentException("Column index out of range");
	}

	@Override
	public String toString() {
		final StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < headers.length; i++)
			buffer.append(headers[i] + "\t");
		buffer.append("\n");

		for (final SoldItem item : rows) {
			buffer.append(item.getId() + "\t");
			buffer.append(item.getName() + "\t");
			buffer.append(item.getPrice() + "\t");
			buffer.append(item.getQuantity() + "\t");
			buffer.append(item.getSum() + "\t");
			buffer.append("\n");
		}

		return buffer.toString();
	}
	
    /**
     * Add new StockItem to table.
     */
	public void addItem(final JPanel panel, final SoldItem item, final boolean historyTab) {

		if (!historyTab) {
			StockItem addStockItem = item.getStockItem();
			if (!checkQuantity(panel, item, item.getQuantity())) {
				return;
			}

			for (SoldItem item2 : rows) {
				if (item2.getStockItem().getId().equals(addStockItem.getId())) {
					Integer newQuantity = item2.getQuantity() + item.getQuantity();
					if (!checkQuantity(panel, item, newQuantity)) {
						return;
					}
					item2.setQuantity(newQuantity);
					log.debug("Renew " + item.getName() + " quantity of " + newQuantity);
					fireTableDataChanged();
					return;
				}
			}

		}
        rows.add(item);
        log.debug("Added " + item.getName() + " quantity of " + item.getQuantity());
		fireTableDataChanged();
    }

	private boolean checkQuantity(final JPanel panel, final SoldItem item, final Integer quantity) {
		if (quantity > item.getQuantity()) {
			log.debug("Nooo, too much!!! " + item.getName() + " quantity of " + quantity);
			JOptionPane.showMessageDialog(panel, "Nooo, too much!!!");
			return false;
		}
		return true;
	}

	public static PurchaseInfoTableModel getEmptyTable() {
		return new PurchaseInfoTableModel();
	}

	/**
	 * Replace the current contents of the table with the SoldItems of the given
	 * Sale. (Used by the history details table in the HistoryTab).
	 */
	public void showSale(Sale sale) {
		this.rows = new ArrayList<SoldItem>(sale.getSoldItems());
		fireTableDataChanged();
	}
}
